package com.jieyang.oa.controller.login;

import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 29276
 * @version 1.0
 * @description: 登录页面
 * @date 2021/8/14 18:30
 */
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {

    @Autowired
    private UserService userService;

    /**
     * @Author 2927624517@qq.com
     * @Description 登录成功，跳转到主页面；登录失败，继续登录
     * @Date 2021/8/28 10:42
     * @Param [req, resp]
     * @return void
    **/
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String empId = req.getParameter("empId");
        String password = req.getParameter("password");
        Employee employee = userService.findByEmpId(empId, password);
        if (null != employee) {
            req.getSession().setAttribute("emp", employee);
//            重定向到主页面
            System.out.println(req.getContextPath()+"/main");
            resp.sendRedirect(req.getContextPath()+"/main.jsp");
        } else {
            req.setAttribute("flog", "用户名或密码错误！");

            System.out.println(req.getContextPath()+"/hello");
            resp.sendRedirect(req.getContextPath()+"/hello");
        }
    }
}
